package com.cloud.message.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.domain.LoginUser;
import com.cloud.entity.SysNotice;
import com.cloud.entity.SysUser;
import com.cloud.enums.BusinessType;
import com.cloud.enums.ReadStatus;
import com.cloud.logs.Logs;
import com.cloud.message.entity.vo.NoticeUserDao;
import com.cloud.message.entity.vo.SysNoticeParam;
import com.cloud.message.service.ISysNoticeService;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.user_info.service.ISysRoleService;
import com.cloud.utils.DateTimeUtils;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.StringUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知信息操作处理
 *
 * @author yzj
 */
@Tag(name = "通知信息")
@RestController
@RequestMapping("/message/notice")
public class SysNoticeController extends BaseController {

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Operation(summary = "查询通知信息列表")
    @RequestPermissions("message:notice:list")
    @GetMapping("/list")
    public Result list(SysNotice notice) {
        startPage();
        notice.setSysCode(SecurityUtils.getSysCode());
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    @Operation(summary = "查询通知详细信息")
    @Parameter(name = "noticeId", description = "通知ID")
    @GetMapping("/getInfo")
    public Result getInfo(Long noticeId) {
        return Result.success("查询成功", noticeService.selectNoticeById(noticeId));
    }

    @Operation(summary = "新增通知信息")
    @Logs(title = "新增通知公告", businessType = BusinessType.INSERT)
    @RequestPermissions("message:notice:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysNotice notice) {
        notice.setDeptId(SecurityUtils.getDeptId());
        notice.setSysCode(SecurityUtils.getSysCode());
        return toResult(noticeService.insertNotice(notice));
    }

    @Operation(summary = "修改通知信息")
    @Logs(title = "修改通知公告", businessType = BusinessType.UPDATE)
    @RequestPermissions("message:notice:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysNotice notice) {
        notice.setSysCode(SecurityUtils.getSysCode());
        return toResult(noticeService.updateNotice(notice));
    }

    @Operation(summary = "删除通知信息")
    @Parameter(name = "noticeIds", description = "通知ID[多个用,隔开]")
    @Logs(title = "删除通知公告", businessType = BusinessType.DELETE)
    @RequestPermissions("message:notice:remove")
    @PostMapping("/remove")
    public Result remove(String noticeIds) {
        List<Long> list = ToolUtils.setIdsToList(noticeIds);
        return toResult(noticeService.deleteNoticeByIds(list));
    }

    @Operation(summary = "Feign公共调用(新增通知信息)")
    @PostMapping("/inAuth/saveInNotice")
    public Result saveInNotice(@RequestBody SysNotice notice) {
        notice.setSysCode(SecurityUtils.getSysCode());
        return toResult(noticeService.insertNotice(notice));
    }

    @Operation(summary = "查询用户相关通知(分页)")
    @GetMapping("/getUserNoticeList")
    public Result getUserNoticeList(SysNotice notice) {
        startPage();
        setNoticeParam(notice, false);
        List<NoticeUserDao> noticeList = noticeService.getUserNoticeList(notice);
        return getDataTable(noticeList);
    }

    @Operation(summary = "查询用户最新通知以及数量")
    @GetMapping("/getUserNoticeCount")
    public Result getUserNoticeCount() {
        SysNotice notice = new SysNotice();
        notice.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", noticeService.getUserNoticeCount(notice));
    }

    @Operation(summary = "清除我的通知信息")
    @Logs(title = "清除我的通知信息", businessType = BusinessType.DELETE)
    @PostMapping("/cleanMyNotice")
    public Result cleanMyNotice(SysNotice notice) {
        setNoticeParam(notice, true);
        return noticeService.cleanUserNotice(notice);
    }

    @Operation(summary = "查询(近1周)用户未读信息")
    @GetMapping("/getUserNotNoticeList")
    public Result getUserNotNoticeList(SysNotice notice) {
        setNoticeParam(notice, true);
        Map<String, Object> map = new HashMap<>();
        List<NoticeUserDao> noticeList = noticeService.getUserNoticeList(notice);
        map.put("noticeList", noticeList);
        map.put("noticeSize", noticeList.size());
        return Result.success("查询成功", map);
    }

    @Operation(summary = "根据类型查询消息列表")
    @Parameter(name = "noticeTypes", description = "消息类型(多个用逗号隔开)")
    @PostMapping("/getNavNoticeList")
    public Result getNavNoticeList(String noticeTypes) {
        if (StringUtils.isEmpty(noticeTypes)) {
            return Result.error("操作失败:查询类型不能为空");
        }
        SysNoticeParam noticeParam = new SysNoticeParam();
        noticeParam.setNoticeTypes(noticeTypes);
        return noticeService.selectNavNoticeList(noticeParam);
    }

    /**
     * 设置公告查询条件
     *
     * @param notice
     * @param isUserScope true 查询关于用户
     */
    private void setNoticeParam(SysNotice notice, boolean isUserScope) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)) {
            SysUser user = loginUser.getUser();
            if (StringUtils.isNotNull(user)) {
                notice.setUserId(user.getUserId());
                notice.setDeptId(user.getDeptId());
            }
            notice.setRoleIds(sysRoleService.selectProjectRoleIds(loginUser.getSysCode(), notice.getUserId()));
        }
        if (isUserScope) {
            notice.setReadStatus(ReadStatus.UNREAD.getCode());
            notice.setBeginTime(DateTimeUtils.getStartDataTime(-6));
            notice.setEndTime(DateTimeUtils.getEndDataTime(0));
        }
        notice.setSysCode(SecurityUtils.getSysCode());
    }

}
